package com.example.service;

import com.example.mapper.CurriculumVitaeMapper;
import com.example.mapper.JobSeekerMapper;
import com.example.pojo.bo.JobSeekerBO;
import com.example.pojo.dto.JobSeekerInformationDTO;
import com.example.pojo.dto.JobSeekerUserInfoDTO;
import com.example.pojo.entity.CurriculumVitae;
import com.example.pojo.entity.JobSeeker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @author 小岁
 * @version 2023/10/1 20:54
 */
@Service
@Transactional
public class JobSeekerServiceImpl implements JobSeekerService{
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    @Autowired
    private CurriculumVitaeMapper curriculumVitaeMapper;

    @Override
    public List<JobSeeker> getAll() {
        return jobSeekerMapper.findAll();
    }

    @Override
    public JobSeeker login(String jsaccount) {
        return jobSeekerMapper.findByJsAccount(jsaccount);
    }

    @Override
    public int register(String jsaccount,String jscipher) {
        return jobSeekerMapper.save(jsaccount,jscipher);
    }

    @Override
    public JobSeeker tblLogin(String jsTelephone) {

        return jobSeekerMapper.findByJsTelephone(jsTelephone);
    }

    @Override
    public int tblRegister(String username, String password, String telephone) {
        return jobSeekerMapper.TblSave(username,password,telephone);
    }

    @Override
    public JobSeekerUserInfoDTO findJSById(Integer id) {
        JobSeekerUserInfoDTO jobSeekerUserInfoDTO = new JobSeekerUserInfoDTO();
        JobSeeker jobSeeker = jobSeekerMapper.findJSById(id);
        CurriculumVitae curriculumVitae = curriculumVitaeMapper.findJsId(id);
        if (null!=jobSeeker.getJobSeek()&&jobSeeker.getJobSeek()==0){
            jobSeekerUserInfoDTO.setJobSeek("职途中");
        }else if (null!=jobSeeker.getJobSeek()&&jobSeeker.getJobSeek()==1){
            jobSeekerUserInfoDTO.setJobSeek("已入职");
        }else if (null!=jobSeeker.getJobSeek()&&jobSeeker.getJobSeek()==2){
            jobSeekerUserInfoDTO.setJobSeek("到处看看");
        }
        if (null!=jobSeeker.getEmployment()&&jobSeeker.getEmployment()==0){
            jobSeekerUserInfoDTO.setEmployment("职场小白");
        }else if (null!=jobSeeker.getEmployment()&&jobSeeker.getEmployment()==1){
            jobSeekerUserInfoDTO.setEmployment("学生");
        }else if (null!=jobSeeker.getEmployment()&&jobSeeker.getEmployment()==2){
            jobSeekerUserInfoDTO.setEmployment("职场精英");
        }else if (null!=jobSeeker.getEmployment()&&jobSeeker.getEmployment()==3){
            jobSeekerUserInfoDTO.setEmployment("职场大佬");
        }
        jobSeekerUserInfoDTO.setBirthday(jobSeeker.getBirthday());
        jobSeekerUserInfoDTO.setId(jobSeeker.getId());
        jobSeekerUserInfoDTO.setJsName(jobSeeker.getJsName());
        jobSeekerUserInfoDTO.setJsEmail(jobSeeker.getJsEmail());
        jobSeekerUserInfoDTO.setJsSex(jobSeeker.getJsSex());
        jobSeekerUserInfoDTO.setJsTelephone(jobSeeker.getJsTelephone());
        jobSeekerUserInfoDTO.setAvatar(jobSeeker.getAvatar());
        if (curriculumVitae==null||curriculumVitae.getPAdvantage()==null){
            jobSeekerUserInfoDTO.setUserGood("发现你的这段内容需继续完善，快去补充吧");
        }else {
            jobSeekerUserInfoDTO.setUserGood(curriculumVitae.getPAdvantage());
        }
        return jobSeekerUserInfoDTO;

    }

    @Override
    public int updateInformation(JobSeekerInformationDTO jobSeekerInformationDTO) {
        JobSeeker jobSeeker = new JobSeeker();
        Date birthdayDate = jobSeekerInformationDTO.getBirthday();

        // 将Date对象转换为LocalDate
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String birthdayString = sdf.format(birthdayDate);
        LocalDate birthday = LocalDate.parse(birthdayString);

        // 当前日期
        LocalDate currentDate = LocalDate.now();
        Period age = Period.between(birthday,currentDate);
        int years = age.getYears();
        jobSeeker.setId(jobSeekerInformationDTO.getId());
        jobSeeker.setJsName(jobSeekerInformationDTO.getJsName());
        jobSeeker.setJobSeek(jobSeekerInformationDTO.getJobSeek());
        jobSeeker.setBirthday(jobSeekerInformationDTO.getBirthday());
        jobSeeker.setEmployment(jobSeekerInformationDTO.getEmployment());
        jobSeeker.setJsSex(jobSeekerInformationDTO.getJsSex());
        jobSeeker.setJsEmail(jobSeekerInformationDTO.getJsEmail());
        jobSeeker.setJsTelephone(jobSeekerInformationDTO.getJsTelephone());
        jobSeeker.setAge(years);
        int count = jobSeekerMapper.updateInformation(jobSeeker);
        return count;
    }

    @Override
    public JobSeeker findJSByIdNew(Integer id) {
        return jobSeekerMapper.findJSById(id);
    }

    @Override
    public int amendJs(Integer id, String name, String telephone, String cipher, String email, Date birthday, Integer sex, String brithplace) {
        Date newDate = new Date();
        LocalDate localDate1 = newDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate localDate2 = birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        Period period = Period.between(localDate2, localDate1); // 计算日期间隔

        Integer age = period.getYears();


        System.out.println("年龄===》");
        System.out.println(newDate.getTime());
        System.out.println(birthday.getTime());
        System.out.println(365 * 24 * 60 * 60 * 1000);
        System.out.println(age);
        JobSeeker jobSeeker = new JobSeeker();
        jobSeeker.setId(id);
        jobSeeker.setJsName(name);
        jobSeeker.setJsTelephone(telephone);
        jobSeeker.setJsCipher(cipher);
        jobSeeker.setJsEmail(email);
        jobSeeker.setBirthday(birthday);
        jobSeeker.setJsSex(sex);
        jobSeeker.setBrithplace(brithplace);
        jobSeeker.setAge(age);
        return jobSeekerMapper.updateInformation(jobSeeker);
    }

    @Override
    public int newAvatar(JobSeeker jobSeeker) {
        return jobSeekerMapper.updateInformation(jobSeeker);
    }


}
